﻿using LD.Vision;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ThresholdFrm : FrmToolBase
    {
        private int selectType = 0;
        public ThresholdFrm()
        {
            InitializeComponent();

            this.Text = TB_Tool.Name;
            this.btn_ok.Click += Btn_ok_Click;
            this.Shown += ToolReadImageFrm_Shown;
        }

        private void ToolReadImageFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var param = Newtonsoft.Json.JsonConvert.SerializeObject(new ThresholdParam()
            {
                MaxThres = (int)this.num_max.Value,
                MinThres = (int)this.num_min.Value,
                Camera = this.cob_camera.Text,
                ThresType= selectType,
            });
            TB_Tool.Param = param;
        }

        private void Init()
        {
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                this.cob_camera.Items.Add(item.Key);
            }
            if (TB_Tool.Param == null)
                return;
            var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ThresholdParam>(TB_Tool.Param);
            if (param != null)
            {
                this.num_max.Value = param.MaxThres;
                this.num_min.Value = param.MinThres;
                this.cob_camera.Text = param.Camera;
                selectType = param.ThresType;

                this.tabControl1.SelectTab(selectType);
            }
        }

        private void btn_upload_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Title = "请选择图片";
                ofd.Filter = "(*.bmp)|*.bmp|(*.jpg) | *.jpg";
                ofd.RestoreDirectory = true;
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string fileName = ofd.FileName;
                    this.txt_imagePath.Text = fileName;
                }
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.tabControl1.SelectTab(0);
        }

        private void rab_carmer_CheckedChanged(object sender, EventArgs e)
        {
            this.tabControl1.SelectTab(1);
        }

        private void rab_image_CheckedChanged(object sender, EventArgs e)
        {
            this.tabControl1.SelectTab(2);
        }
    }
}
